### Project 27: Button-controlled LED ![](media/50740b22d16151d490b8494b0bff4f6e.jpeg) **Overview** In this lesson, we will make an extension experiment with a button and an LED. When the button is pressed and low levels are output, the LED will light up; when the button is released, the LED will go off. Then we can control a module with another module. **Components** ![image-20231020085222290](media/image-20231020085222290.png) **Connection Diagram** ![](media/378de9cb95275a6a1dec9adbf2f15eaa.png) **Test Code** ```c //********************************************************************** /* * Filename : button_control_LED * Description : Make a table lamp. * Auther : http//www.keyestudio.com */ #define PIN_LED 4 #define PIN_BUTTON 15 bool ledState = false; void setup() { // initialize digital pin PIN_LED as an output. pinMode(PIN_LED, OUTPUT); pinMode(PIN_BUTTON, INPUT); } // the loop function runs over and over again forever void loop() { if (digitalRead(PIN_BUTTON) == LOW) { delay(20); if (digitalRead(PIN_BUTTON) == LOW) { reverseGPIO(PIN_LED); } while (digitalRead(PIN_BUTTON) == LOW); } } void reverseGPIO(int pin) { ledState = !ledState; digitalWrite(pin, ledState); } //********************************************************************** ``` **Test Result** Connect the wires according to the experimental wiring diagram, compile and upload the code to the ESP32. After uploading successfully,we will use a USB cable to power on. When the button is pressed, the LED will light up; when pressed again, the LED will go off.